/**
 * Created with IntelliJ IDEA.
 * User: Tung An
 * Date: 12/11/13
 * Time: 1:30 PM
 * To change this template use File | Settings | File Templates.
 */

var Header = {
    init: function () {
        this.show();
    },

    onClick: function (event) {
        switch (event.target.data) {
            case "create-look":
                this.checkLogin();
                break;
            case "search":
                this.getSearchLook();
                break;
            case "gologin":
                LoginPopup.init();
                break;
            case "log-out":
                this.logout();
                addClass($class("account-menu")[0], "hide");
                $class("account-info")[0].remove();
                var loginNode = document.createElement("div");
                loginNode.setAttribute("data", "gologin");
                loginNode.className = "button login";
                loginNode.innerText = "Sign in";
                $id("account-wrapper").appendChild(loginNode);
                break;
            case "account-info":
                if (hasClass($class("account-menu")[0], "hide")) {
                    removeClass($class("account-menu")[0], "hide");
                } else {
                    addClass($class("account-menu")[0], "hide");
                }
                break;
            case "manage":
                window.location = "/manage_look.jsp?id=" + parseInt($class("username")[0].id);
                break;
            default:
                break;
        }
    },

    logout: function () {
        var success = function (resp) {
            if (MainPage.isVisible) {
                MainPage.getListLooks();
            } else if (ManagerLook.isVisible) {
                window.location = "/";
            }
        };

        var failure = function () {
        };

        ajax.post({
            url: "LoginServlet",
            param: {action: "logout"},
            success: success,
            failure: failure
        });
    },

    checkLogin: function (lookID) {
        var success = function (resp) {
            if (resp && resp.querySelector("account").firstChild) {
                window.location = "upload_image.jsp";
            } else {
                LoginPopup.isCreate = true;
                LoginPopup.init();
            }
        };

        var failure = function () {
        };

        ajax.post({
            url: "LoginServlet",
            param: {action: "checklogged"},
            success: success,
            failure: failure
        });
    },

    show: function () {
        this.listener = function (event) {
            var node = event.target;
            event.target.data = xmlUtils.getNodeWithData(node);
            Header.onClick(event);
        };
        $id("header").addEventListener("click", this.listener, false);
    },

    getSearchLook: function (){
        var text = document.getElementById("keyword-input").value;

        var success = function (resp) {
            var userId = null;
            if ($class("username")[0] && $class("username")[0].id) {
                userId = $class("username")[0].id;
            }

            var html = xmlUtils.transformXSL(resp, "templates/looks.xsl", [
                {key: "userId", value: userId}
            ]);
            $id('list-look').innerHTML = "";
            $id('list-look').appendChild(html);
            setTimeout(function () {
                GridFormat.refreshLayout();
            }, 100);
        };

        var failure = function () {

        };

        ajax.post({
            url: "ProcessServlet",
            param: {action: "search", text : text},
            success: success,
            failure: failure
        });
    }
};